using System;
using bitzhuwei.Compiler;

namespace bitzhuwei.AnsiCFormat {
    /// <summary>
    /// Correspond to the Vn node specifier_qualifier_list in the grammar(AnsiC).
    /// </summary>
    internal partial class Vnspecifier_qualifier_list : IFullFormat {
        // [111] specifier_qualifier_list = type_specifier specifier_qualifier_list ;
        // [112] specifier_qualifier_list = type_specifier ;
        // [113] specifier_qualifier_list = type_qualifier specifier_qualifier_list ;
        // [114] specifier_qualifier_list = type_qualifier ;


        private readonly List<IFullFormat> prefix = new();
        private readonly IFullFormat complex;

        private readonly TokenRange _scope;
        public TokenRange Scope => _scope;
        public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
            var config = new BlankConfig();
            var prefixCount = this.prefix.Count;
            for (var i = 0; i < prefixCount; i++) {
                context.PrintBlanksAnd(this.prefix[i], i == 0 ? preConfig : config, writer);
                if (i + 1 < prefixCount) {
                    context.PrintCommentsBetween(this.prefix[i], this.prefix[i + 1], config, writer);
                }
                else {
                    context.PrintCommentsBetween(this.prefix[i], this.complex, config, writer);
                }
            }
            context.PrintBlanksAnd(this.complex, prefixCount == 0 ? preConfig : config, writer);
        }

        // [111] specifier_qualifier_list = type_specifier specifier_qualifier_list ;
        public void Insert(Vntype_specifier r0) {
            this.prefix.Insert(0, new Prefix0(r0));
            this._scope.start = r0.Scope.start;
        }
        class Prefix0 : IFullFormat {
            private readonly TokenRange _scope;
            public TokenRange Scope => _scope;
            public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
                context.PrintBlanksAnd(r0, preConfig, writer);
            }
            public readonly Vntype_specifier r0;
            public Prefix0(Vntype_specifier r0) {
                this.r0 = r0;
                this._scope = new TokenRange(r0);
            }
        }

        // [113] specifier_qualifier_list = type_qualifier specifier_qualifier_list ;
        public void Insert(Vntype_qualifier r0) {
            this.prefix.Insert(0, new Prefix1(r0));
            this._scope.start = r0.Scope.start;
        }
        class Prefix1 : IFullFormat {
            private readonly TokenRange _scope;
            public TokenRange Scope => _scope;
            public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
                context.PrintBlanksAnd(r0, preConfig, writer);
            }
            public readonly Vntype_qualifier r0;
            public Prefix1(Vntype_qualifier r0) {
                this.r0 = r0;
                this._scope = new TokenRange(r0);
            }
        }


        // [112] specifier_qualifier_list = type_specifier ;
        public Vnspecifier_qualifier_list(Vntype_specifier r0) {
            this.complex = new complex0(r0);
            this._scope = new TokenRange(r0);
        }
        class complex0 : IFullFormat {
            private readonly TokenRange _scope;
            public TokenRange Scope => _scope;
            public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
                context.PrintBlanksAnd(r0, preConfig, writer);
            }
            public readonly Vntype_specifier r0;
            public complex0(Vntype_specifier r0) {
                this.r0 = r0;
                this._scope = new TokenRange(r0);
            }
        }

        // [114] specifier_qualifier_list = type_qualifier ;
        public Vnspecifier_qualifier_list(Vntype_qualifier r0) {
            this.complex = new complex1(r0);
            this._scope = new TokenRange(r0);
        }
        class complex1 : IFullFormat {
            private readonly TokenRange _scope;
            public TokenRange Scope => _scope;
            public void FullFormat(BlankConfig? preConfig, TextWriter writer, FormatContext context) {
                context.PrintBlanksAnd(r0, preConfig, writer);
            }
            public readonly Vntype_qualifier r0;
            public complex1(Vntype_qualifier r0) {
                this.r0 = r0;
                this._scope = new TokenRange(r0);
            }
        }



    }
}
